#include "Common/Common.h"
#include "PropertyGridPopulator.h"
#include "PropertyMapper.h"

using namespace cmn;

PropertyGridPopulator::PropertyGridPopulator() {}
PropertyGridPopulator::~PropertyGridPopulator() {}

void PropertyGridPopulator::PopulateWithEntity( wxPropertyGrid* propertyGrid, editor_core::IEntity* entity ) {
	propertyGrid->Clear();

	Vector<editor_core::Property> properties;
	entity->GetProperties( properties );

	for( IteratorPtr<editor_core::Property> propertiesIter( properties );
			!propertiesIter.IsEnd();
			propertiesIter.Next() )
	{
		AppendProperty( propertyGrid, propertiesIter.Current() );
	}
}

void PropertyGridPopulator::AppendProperty( wxPropertyGrid* propertyGrid, const editor_core::Property& property ) {
	PropertyMapper mapper;
	wxPGProperty* prop = propertyGrid->Append( mapper.ToWxProperty( property ) );

	Vector<editor_core::Property> children;
	property.GetChildren( children );

	if( children.GetLength() > 0 ) {
		for( IteratorPtr<editor_core::Property> propertiesIter( children );
				!propertiesIter.IsEnd();
				propertiesIter.Next() )
		{
			AppendChildProperty( propertyGrid, prop, propertiesIter.Current() );
		}
	}
}

void PropertyGridPopulator::AppendChildProperty( wxPropertyGrid* propertyGrid, wxPGProperty* parentProperty, const editor_core::Property& property ) {
	PropertyMapper mapper;
	wxPGProperty* prop = propertyGrid->AppendIn( parentProperty, mapper.ToWxProperty( property ) );

	Vector<editor_core::Property> children;
	property.GetChildren( children );

	if( children.GetLength() > 0 ) {
		for( IteratorPtr<editor_core::Property> propertiesIter( children );
				!propertiesIter.IsEnd();
				propertiesIter.Next() )
		{
			AppendChildProperty( propertyGrid, prop, propertiesIter.Current() );
		}
	}
}
